﻿using INTEX.DataBase;
using INTEX.DataBase.Tools;
using ReflectionStudio.Core.Events;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Threading;
using WPFExcelReport;
namespace INTEX.DataBase.Tools
{
    public partial class INTEXBL
    {
        void DoJLTAB50MUWork()
        {
            if (IsRun)
            {
                return;
            }
            IsRun = true;
            string path = @"C:\IntexReport\Data\JLTAV95";
            string ITXDBVERSION = "JLT";
            JLTAB50MU(ITXDBVERSION, path);
        }



        public void JLTAB50MU(string ITXDBVERSION, string path)
        {
            try
            {
                EventDispatcher.Instance.RaiseStatus("JLTAV95启动", StatusEventType.StartProgress);
                List<DataRow> list;
                while (true)
                {
                    DataSet SetJob = GetJob(path);
                    if (SetJob.Tables.Count == 0)
                    {
                        IsRun = false;
                        break;
                    }
                    list = (List<DataRow>)CreateAB50(ITXDBVERSION, SetJob);
                    if (list == null)
                        return;
                    string KRS = "", LFD = "", POS = "";
                    string KKRS = "", KLFD = "", KPOS = "";
                    string procecesVK = "";
                    foreach (DataRow newrow in list)
                    {
                        if (newrow.Table.TableName == "ABPOS")
                        {
                            KRS = newrow["ABPAUFKRS"].ToString();
                            LFD = newrow["ABPAUFLFD"].ToString();
                            POS = newrow["ABPAUFPOS"].ToString();

                            DataRow dwData = INTEXDB.ICC_QUERYTB.NewRow();

                            var qABA = from DataRow row in SetJob.Tables["ICC_ABAX"].AsEnumerable()
                                       where row["ABAAUFKRS"].ToString() == KRS
                                       && row["ABAAUFLFD"].ToString() == LFD
                                       && row["ABAAUFPOS"].ToString() == POS
                                       select row;
                            foreach (DataRow row in qABA)
                            {
                                KKRS = row["ABAVKAUFKRS"].ToString();
                                KLFD = row["ABAVKAUFLFD"].ToString();
                                KPOS = row["ABAVKAUFPOS"].ToString();
                                if (procecesVK.Contains(KKRS + KLFD + KPOS + ";")) continue;
                                procecesVK += KKRS + KLFD + KPOS + ";";
                                dwData["ZUMANDANT"] = MANDANT;
                                dwData["ZUAUFKRS"] = row["ABAVKAUFKRS"];
                                dwData["ZUAUFLFD"] = row["ABAVKAUFLFD"];
                                dwData["ZUAUFPOS"] = row["ABAVKAUFPOS"];
                                INTEXDBC.DeleteZUTEI(dwData, 2);
                                dwData["GAMANDANT"] = MANDANT;
                                dwData["GAAUFKRS"] = row["ABAVKAUFKRS"];
                                dwData["GAAUFLFD"] = row["ABAVKAUFLFD"];
                                dwData["GAAUFPOS"] = row["ABAVKAUFPOS"];
                                dwData["GASA"] = "713";
                                INTEXDBC.UpdateGAAUF(dwData, 3);
                                dwData["ABPMANDANT"] = MANDANT;
                                dwData["ABPAUFKRS"] = row["ABAVKAUFKRS"];
                                dwData["ABPAUFLFD"] = row["ABAVKAUFLFD"];
                                dwData["ABPAUFPOS"] = row["ABAVKAUFPOS"];
                                INTEXDBC.UpdateABPOS(dwData, 6);
                            }
                        }
                    }
                }
                EventDispatcher.Instance.RaiseStatus("JLTAV95完成", StatusEventType.StopProgress);
            }
            catch (Exception ex)
            {
                EventDispatcher.Instance.RaiseStatus("JLTAV95异常终止", StatusEventType.StopProgress);
                Tracer.Error(ex.StackTrace, ex);
            }
            finally
            {
                IsRun = false;
            }
        }
     
    }
}